﻿/**
 * @file mainwindow.cpp
 * @author huangxb
 * @email 18228033207@163.com
 * @date 2025-04-08
 * @brief 主窗口视图
 */
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "maincontroller.h"
#include <QSplitter>
#include <QTreeView>
#include <QStandardItemModel>
#include <QStyleFactory>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    SetupUI();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::SetController(CMainController *pController)
{
    m_pController = pController;
    UpdateUI();
}

void MainWindow::ShowComponentView(QWidget *pView)
{
    if(!pView)
    {
        return;
    }
    auto pReplaceWidget = m_pSplitter->replaceWidget(1, pView);
    if(pReplaceWidget != m_pEmptyView)
    {
        delete pReplaceWidget;
    }
}

void MainWindow::OnComponentTreeDoubleClicked(const QModelIndex &index)
{
    m_pController->DoubleClickComponent(index);
}

void MainWindow::SetupUI()
{
    ui->setupUi(this);

    m_pSplitter = new QSplitter(Qt::Horizontal, this);
    m_pComponentTreeView= new QTreeView(m_pSplitter);
    m_pEmptyView = new QWidget();

    m_pComponentTreeView->setStyle(QStyleFactory::create("windows"));

    m_pSplitter->addWidget(m_pComponentTreeView);
    m_pSplitter->addWidget(m_pEmptyView);

    setCentralWidget(m_pSplitter);

    connect(m_pComponentTreeView, &QTreeView::doubleClicked, this, &MainWindow::OnComponentTreeDoubleClicked);
}

void MainWindow::UpdateUI()
{
    Q_ASSERT(m_pController);

    m_pController->InitComponentModel();
    m_pComponentTreeView->setModel(m_pController->GetComponentModel());
}
